Ext.setup( {
	icon : 'icon.png',
	tabletStartupScreen : 'tablet_startup.png',
	phoneStartupScreen : 'phone_startup.png',
	glossOnIcon : false,
	onReady : function() {

		var form;
		
		 Ext.regModel('sealapply', {
	       	    fields: [
	       	        {name: 'AGENCY', type: 'string'},
	       	        {name: 'SEALNAME', type: 'string'},
	    	        {name: 'QUANTITY', type: 'string'},
	    	        {name: 'DEPARTMENT', type: 'string'},
	    	        {name: 'DESCRIBE', type: 'string'},
	    	        {name: 'TRUSTEES', type: 'string'},
	    	        {name: 'COMMITER', type: 'string'}
	       	    ],
	       	 validations: [ 
	       	               	  {type: 'presence', name: 'AGENCY'},
				              {type: 'presence', name: 'SEALNAME'},
				              {type: 'presence', name: 'DEPARTMENT'},
				              {type: 'presence', name: 'DESCRIBE'},
				             {type: 'format',   name: 'QUANTITY', matcher: /^[0-9]*[1-9][0-9]*$/}
				          ]
	       	});
		 
		 Ext.Ajax.request({
             url: '/wipzIphone/dic/dic_userAndDepariment.action',
             success: function(response, opts) {
        	     var jsonObj = eval("(" + response.responseText + ")");
        	    	var user = Ext.ModelMgr.create({
        	    		DEPARTMENT : jsonObj.DEPARTMENT,
        	    		COMMITER : jsonObj.COMMITER
        	    	}, 'sealapply');
        	    
        	    	form.load(user);
             }
         });

		
		 Ext.regModel('agenymapmodel', {
       	    fields: [
       	        {name: 'AGENCYID', type: 'string'},
       	        {name: 'NAME', type: 'string'}
       	    ]
       	});
  	
  	var agenymapStore = new Ext.data.Store({
  		autoLoad: true,
  	    model: 'agenymapmodel',
  	    proxy: {
  	        type: 'ajax',
  	        url : '/wipzIphone/dic/dic_agencyMap.action',
  	        reader: {
  	            type: 'json'
  	        }
  	    }
  	});
  	
  	 Ext.regModel('sealmapmodel', {
    	    fields: [
    	        {name: 'SEALCODE', type: 'string'},
    	        {name: 'SEALNAME', type: 'string'}
    	    ]
    	});
  	
  	var sealNameStore = new Ext.data.Store({
  		autoLoad: true,
  	    model: 'sealmapmodel',
  	    proxy: {
  	        type: 'ajax',
  	        url : '/wipzIphone/dic/dic_sealName.action',
  	        reader: {
  	            type: 'json'
  	        }
  	    }
  	});
  	
		
		var formBase = {
			id: 'addofficeSupplyApply', 
			scroll : 'vertical',
			url : 'postUser.php',
			standardSubmit : false,
			items : [ {
				xtype : 'fieldset',
				title : '所属单位',
				items : [ {
					xtype : 'selectfield',
					name : 'AGENCY',
					label : '单位名称',
					placeHolder : '请选择所属单位',
					valueField: 'AGENCYID',
					displayField:'NAME',
					store : agenymapStore,
					required : true
				} ]
			}, {
				xtype : 'fieldset',
				title : '基本信息',
				defaults : {
					required : true,
					labelAlign : 'left',
					labelWidth : '40%'
				},
				items : [  {
					xtype : 'selectfield',
					name : 'SEALNAME',
					label : '用印名称',
					placeHolder : '请选择用印名称',
					valueField: 'SEALCODE',
					displayField:'SEALNAME',
					store : sealNameStore,
					listeners: {
					change: function (a,b){
					 Ext.Ajax.request({
			             url: '/wipzIphone/dic/dic_sealName.action?sealcode='+b,
			             success: function(response, opts) {
			        	     var jsonObj = eval("(" + response.responseText + ")");
			        	     if(jsonObj.length > 0) {
			        	    	Ext.getCmp('TRUSTEES').setValue(jsonObj[0].TRUSTEES);
			        	    	Ext.getCmp('TRUSTEESCODE').setValue(jsonObj[0].TRUSTEESCODE);
			        	    	Ext.getCmp('SEALTYPE').setValue(jsonObj[0].SEALTYPE);
			        	    	Ext.getCmp('TRUSTEES').setDocked();
			        	     }
			             }
			         });
							}
						},
					required : true
				}, {
					xtype : 'textfield',
					id : 'TRUSTEES',
					label : '经办人',
					useClearIcon : false,
					required : true
				},
				{
					xtype : 'textfield',
					id : 'TRUSTEESCODE',
					label : '经办人编号',
					hidden : true
				},
				{
					xtype : 'textfield',
					id : 'SEALTYPE',
					label : '用印类型',
					hidden : true
				},
				{
					xtype : 'textfield',
					id : 'COMMITER',
					label : '申请人',
					hidden : true
				},{
					xtype : 'textfield',
					name : 'DEPARTMENT',
					label : '申请部门',
					useClearIcon : false,
					required : true
				},{
					xtype: 'spinnerfield',
					name : 'QUANTITY',
					minValue: 1,
			        maxValue: 1000,
			        cycle:true,
			        required:true,
			        startValue:1,
			        inputType:'number',
					label : '用印份数'
				} ,
				{
                    xtype : 'textareafield',
                    name  : 'DESCRIBE',
                    label : '用印事由',
                    maxLength : 60,
                    maxRows : 3
                }]
			} ],
			listeners : {
				submit : function(form, result) {
					console.log('success', Ext.toArray(arguments));
				},
				exception : function(form, result) {
					console.log('failure', Ext.toArray(arguments));
				}
			},
			

			dockedItems : [ {
				xtype : 'toolbar',
				dock : 'bottom',
				items : [  {
					xtype : 'spacer'
				}, {
					text : '取消',
					handler : function() {
					document['success'] = 'false';
					}
				},  {
					text : '提交',
					ui : 'confirm',
					handler : function() {
					var currentNote = form.getRecord(); 
		             form.updateRecord(currentNote); 
	 
	                var errors = currentNote.validate(); 
	                if (!errors.isValid()) { 
	             Ext.Msg.alert('等待...', "带*号选项为必填信息且用印份数必须为大于0的数字！", Ext.emptyFn); 
	                    return; 
	                } 
					  
					Ext.Msg.confirm("提示", "确认对以上信息进行提交吗？", function(a) {
					if(a+""=="yes"){
						form.submit( {
							waitMsg : {
								message : '正在提交...',
								cls : 'demos-loading'
							},
						 url: "/wipzIphone/seal/sealApply_insert.action",           
						 method: 'POST',           
						 success: function(thisForm, action){ 
								Ext.Msg.alert('提示', '提交成功，等待下级审批人‘'+action.NEXTOPERATOR+'’审批！', function() {
									document['success'] = 'true';
								});
								unLoadMask(cmpMask);  
								}
							});
						}else {
							return ;
						}
					}
					);
					}
				}]
			} ]
		};

		if (Ext.is.Phone) {
			formBase.fullscreen = true;
		} else {
			Ext.apply(formBase, {
				autoRender : true,
				floating : true,
				modal : true,
				centered : true,
				hideOnMaskTap : false,
				height : 385,
				width : 480
			});
		}

		form = new Ext.form.FormPanel(formBase);
		form.show();
	}
});